package com.yiwyn.aspect;

import com.yiwyn.util.MapUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.PrimitiveIterator;

@Component
@Aspect
@Slf4j
public class ParamLogAspect {

    @Pointcut("@annotation(com.yiwyn.annontation.ParamLog)")
    public void paramLogPointcut() {
    }


    @Around("paramLogPointcut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object proceed = null;

        try {
            // 获取参数
            Object[] args = proceedingJoinPoint.getArgs();

            // 获取参数名
            MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
            String[] parameterNames = signature.getParameterNames();

            // 获取方法名
            String methodName = signature.getName();

            Map<String, Object> valueMap = MapUtil.joinArray(parameterNames, args);

            log.info("{}:{}", methodName, valueMap);
        } catch (Throwable e) {
            System.out.println("出现了错误:" + e.getMessage());
        }
        proceed = proceedingJoinPoint.proceed();

        return proceed;
    }


}
